package com.xuan.controller;

import com.xuan.utils.CaptchaUtil;
import com.xuan.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private CaptchaUtil captchaUtil;

    /**
     *  生成验证码图片
     * @param key  唯一标识（前端传递）
     * @param response  响应对象
     */
    @GetMapping
    public void getCaptcha(
        @RequestParam String key,
        HttpServletResponse response
    ) {
        // 这里没有前端 就用uuid模拟一下
        key = UUIDUtil.generate();
        System.err.println(key);
        response.setHeader("captchaCode", key);
        captchaUtil.generateCaptcha(key, response);
    }
}